﻿using SLClient.CustomerControls;
using SLClient.Extension;
using SLClient.ServiceAPI;
using SLClient.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;

namespace SLClient.Views
{
    public partial class LowerManagerView : Page
    {

        private List<NavUserInfo> list;
        private int pos;

        public LowerManagerView()
        {
            this.InitializeComponent();
            base.DataContext=new LowerManagerViewModel();
            this.list = new List<NavUserInfo>();
            this.navControl.SetNavigation(this.list);
        }

        private void btAddLower_Click(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LowerManagerViewModel).AddLower();
        }

        private void btChange_Click(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LowerManagerViewModel).EditLower();
        }

        private void btRefreshList_Click(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LowerManagerViewModel).RefreshList(ref this.pos);
        }

        private void btSearchList_Click(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LowerManagerViewModel).SearchList(ref this.pos);
        }

        private void DgListLoadingRow(object sender, DataGridRowEventArgs e)
        {
            UserInfo info = e.Row.DataContext as UserInfo;
            if (info.UserId != Guid.Empty)
            {
                int num = ++this.pos;
                (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text=num.ToString();
                FrameworkElement cellContent = this.dgList.Columns[3].GetCellContent(e.Row);
                cellContent.Visibility = Visibility.Visible;
                (cellContent.FindName("imgArrowUp") as Image).Visibility=(info.UserId == ViewDispatcher.LoginUser.UserId) ? ((Visibility)1) : ((Math.Min(ViewDispatcher.CurrentConfig.MaxSiteFiandian, info.MaxFiandian) > info.Fandian) ? ((Visibility)0) : ((Visibility)1));
                this.dgList.Columns[8].GetCellContent(e.Row).Visibility = Visibility.Visible;
                this.dgList.Columns[9].GetCellContent(e.Row).Visibility = Visibility.Visible;
                FrameworkElement element2 = this.dgList.Columns[10].GetCellContent(e.Row);
                element2.Visibility = Visibility.Visible;
                (element2.FindName("btChange") as Button).Visibility=(info.UserId != ViewDispatcher.LoginUser.UserId) ? ((Visibility)0) : ((Visibility)1);
            }
            else
            {
                (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text="";
                this.dgList.Columns[2].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[3].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[8].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[9].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[10].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
            }
        }

        private void navControl_LowerClicked(object sender, NavUserInfo nui)
        {
            (base.DataContext as LowerManagerViewModel).GetChildLowerList(nui.UserId, ref this.pos, ref this.list);
            this.navControl.SetNavigation(this.list);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void TextBlockMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            (base.DataContext as LowerManagerViewModel).GetChildLowerList(Guid.Empty, ref this.pos, ref this.list);
            this.navControl.SetNavigation(this.list);
        }

    }
}
